
/**
 * @author Troy Gardner
 */
class com.troyworks.framework.ui.RandomCharacterGenerator extends MovieClip {

	var config : Object;
	var characters : Array;
	var _owidth : Number;
	var _oheight : Number;

	private var _ox : Number;

	private var _oy : Number;
	public function RandomCharacterGenerator() {
	  super();
		characters = new Array();
	}
	public function onLoad() : Void{
		trace("onLoad");
		_owidth = _width;
		_oheight = _height;
		_ox = _x;
		_oy = _y;
		var characteRoster : Array = new Array();
		var c = config.numberOfTypesOfCharacters;
		var link_ary : Array = config.linkageIDs.concat();
		while(c--){
			var a = Math.round( Math.random()*(link_ary.length-1));
			var act = link_ary[a];
			link_ary.splice(a,1);
			trace("adding " + a + " " + act);
			characteRoster.push(act);
		}
		var minScale : Number = config.minScale;
		var difScale : Number = config.maxScale - config.minScale;
		////attach particles
		var cnt = Number(config.numberOfCharacters);
		while(cnt--){
			var i = Math.round(Math.random()* (characteRoster.length -1));
			var link : String = characteRoster[i];
			trace("using character " + link);
			var initObj : Object = new Object();
			trace("THIS W " + _owidth + " H " + _oheight);
			initObj._x = Math.random() * _owidth * .9 ;
			initObj._y = Math.random() * _oheight * .9;
			trace("using X " + initObj._x + " " + initObj._y);
			initObj._ox = initObj._x;
			initObj._oy = initObj._y;
			var rndS : Number = Math.random()* difScale;
			initObj._xscale = minScale + rndS;
			initObj._yscale = minScale + rndS;
			initObj.id = cnt;
			initObj.friction = (config.friction == null)?.95:config.friction;
			initObj.vx = Math.random()*config.speed - config.speed/2;
			initObj.vy = Math.random()*config.speed - config.speed/2;
			var _mc:MovieClip = this.attachMovie(link, link+cnt,this.getNextHighestDepth(), initObj);
			rangeCheckX(_mc);
			rangeCheckY(_mc);
			characters.push(_mc);
		}
		trace("charactersp  " + characters.length + " \r\t " + characters.join("\r\t"));
		
	}
	public function activate():Void{
		onEnterFrame = onEnterFrameHandler;
		
	}
	public function deactivate():Void{
		onEnterFrame = null;
		delete(onEnterFrame);
	}
	
	public function onEnterFrameHandler() : Void{
		trace("animate");
		for (var i : Number = 0; i < characters.length; i++) {
			var _mc : MovieClip = MovieClip(characters[i]);
			
			 if (Math.random()<config.jitter) {
				_mc.vx += Math.random()*config.speed-config.speed/2;
				_mc.vy += Math.random()*config.speed-config.speed/2;
			  }
			  if(config.mouseAttraction !=0 && config.mouseXpadding < _xmouse && _xmouse < (_width -config.mouseXpadding) && config.mouseYpadding < _ymouse && _ymouse < _height -config.mouseYpadding){
				//trace("Mouse is Over");
				var xdif = _mc._x - _xmouse;
				var ydif = _mc._y - _ymouse;
				var dist : Number = Math.sqrt(xdif*xdif+ydif*ydif);
				if(dist <= config.mouseSphere){
					var activation : Number = Math.max(.01, Math.min(dist/config.mouseSphere, 1));
					var att = (Math.cos(Math.PI/2 *activation)* config.mouseAttraction) -.8;
				//trace(_mc.id + "dist " + dist + " " + activation + " att " + att);
					_mc.vx -= ((dist > 0)? att: -1 * att)*(xdif >0)?1:-1;
					_mc.vy -= ((dist > 0)? att: -1 * att)*(ydif >0)?1:-1;
				}
			  	
			  }
			  //limit on speed.
			  if(config.maxSpeed != 0){
				_mc.vx = Math.min(_mc.vx, config.maxSpeed);
				_mc.vy = Math.min(_mc.vy, config.maxSpeed);
			  }
			var px = _mc._x;
			var py = _mc._y;
			_mc._x +=  _mc.vx;
			_mc._y +=  _mc.vy;
			
		//	_mc.vx *= _mc.friction;
		//	_mc.vy *= _mc.friction;
			var dX = _mc._x - px;
			var dY = _mc._y - py;

			var theta = Math.atan2(dY, dX) *180 / Math.PI  ;
			var t60 = (theta < 0)?theta + 360: theta;
//trace(" theta " + theta + " " + t60);

			if(235 <t60 && t60 <= 315){
				_mc.gotoAndStop("up");
				_mc._rotation = theta +90;
				//_mc._rotation =0;
			}else if(45 <t60 && t60 <= 135){
				_mc.gotoAndStop("down");
				_mc._rotation = theta -90; 
			}else if(135<  t60 &&  t60 <=235 ){
				_mc.gotoAndStop("left");
				_mc._rotation = 0;
			}else{// if(315< t60  && t60 <= 45){
				_mc.gotoAndStop("right");
				_mc._rotation = 0;
			}
			/*if(_mc.vx >0){
				_mc._width = -1 * _mc._width;
			}else{
				_mc._width = Math.abs( _mc._width);
				
			}*/

			  ////////////////////////////////
			  // Range check  //////////////
			  /////////////////////////////
			//_mc._alpha = 20;
			var b : Object = _mc.getBounds(this);
			rangeCheckX(_mc, b);
			rangeCheckY(_mc, b);


			}
	}
	public function rangeCheckX(_mc : MovieClip, bounds : Object) : Boolean{
		
	//	trace("RangeCheckX 0[" + _mc.id + " X " + _mc._x + " X2 "  + (_mc._x + _mc._width) + "  ]" + _owidth);
			  if (bounds.xMin  < 0){
			//		trace("BELOW X");
					//	_mc._alpha = 100;
			//		_mc._alpha = 30;
			_mc.vx = Math.abs(_mc.vx) + Math.random()* 2;
			var adj : Number = (bounds.xMin < 0)? bounds.xMin:0;
				//	_mc._x =  0 -adj + _mc.vx;
			return false;
			  }else if(_owidth < bounds.xMax ) {
			//		trace("****ABOVE W");
			//			_mc._alpha = 30;
			_mc.vx = (Math.abs(_mc.vx) + Math.random()* 2) * -1;
			var adj : Number = (bounds.xMax > 0)? bounds.xMax - _mc._x:0;
				//	_mc._x =  _owidth -  adj;
			return false;
			  }else{
			//  	_mc._alpha = 100;
		//	  	trace("PASSED " + _owidth);
			  }
		return true;
	}
	public function rangeCheckY(_mc : MovieClip, bounds : Object) : Boolean{
		//trace("RangeCheckY " + _mc.id + " Y " + _mc._y + " Y2 " + (_mc._y + _mc._height));
			  if (bounds.yMin  < 0){
				 // 	trace("BELOW Y");
			_mc.vy =  Math.abs(_mc.vy);
			//	 var adj:Number = (bounds.yMin < 0)? bounds.yMin:0;
			//	_mc._y =  0 -adj + _mc.vy;
			return false;
			  }else if( _oheight < bounds.yMax){
			  	//	trace("****ABOVE Y");
			_mc.vy = Math.abs(_mc.vy) * -1;
		//		var adj:Number = (bounds.yMax < 0)? bounds.yMax:0;
		//		_mc._y =  _oheight -  adj;
			return false;
			  }
		return true;
	}
	public function onUnload() : Void{
		trace("onOnLoad");
		
	}
}